package com.yc.cloud.admin.controller;

import com.yc.cloud.admin.dto.RecordingConfig;
import com.yc.cloud.admin.service.StorageStrategyResolver;
import com.yc.cloud.admin.service.HwCameraStorageStrategyService;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.entity.HwCameraStorageStrategy;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 摄像头录制控制器
 *
 * @author kinggu
 * @since 2025-09-16
 */
@RestController
@RequestMapping("/camera-recording")
@Tag(name = "摄像头录制管理", description = "摄像头录制策略和配置管理")
@Slf4j
public class CameraRecordingController {

    @Resource
    private StorageStrategyResolver strategyResolver;

    @Resource
    private HwCameraStorageStrategyService strategyService;

    @GetMapping("/config/{cameraId}")
    @Operation(summary = "获取摄像头当前录制配置", description = "获取指定摄像头当前时间的录制配置信息")
    public ResponseResult<RecordingConfig> getCurrentRecordingConfig(
            @Parameter(description = "摄像头ID") @PathVariable Long cameraId) {
        
        log.info("获取摄像头 {} 的录制配置", cameraId);
        RecordingConfig config = strategyResolver.getCurrentRecordingConfig(cameraId);
        return ResponseResult.success(config);
    }

    @GetMapping("/strategy/{cameraId}")
    @Operation(summary = "获取摄像头有效策略", description = "获取指定摄像头当前有效的存储策略")
    public ResponseResult<HwCameraStorageStrategy> getEffectiveStrategy(
            @Parameter(description = "摄像头ID") @PathVariable Long cameraId) {
        
        log.info("获取摄像头 {} 的有效策略", cameraId);
        HwCameraStorageStrategy strategy = strategyResolver.getEffectiveStrategy(cameraId);
        return ResponseResult.success(strategy);
    }

    @PostMapping("/bind")
    @Operation(summary = "绑定摄像头策略", description = "为摄像头绑定指定的存储策略")
    public ResponseResult<Boolean> bindCameraStrategy(
            @Parameter(description = "摄像头ID") @RequestParam Long cameraId,
            @Parameter(description = "策略ID") @RequestParam Long strategyId) {
        
        log.info("为摄像头 {} 绑定策略 {}", cameraId, strategyId);
        boolean result = strategyService.bindCameraStrategy(cameraId, strategyId);
        return ResponseResult.success(result);
    }

    @DeleteMapping("/unbind/{cameraId}")
    @Operation(summary = "解除摄像头策略绑定", description = "解除指定摄像头的策略绑定，将使用全局默认策略")
    public ResponseResult<Boolean> unbindCameraStrategy(
            @Parameter(description = "摄像头ID") @PathVariable Long cameraId) {
        
        log.info("解除摄像头 {} 的策略绑定", cameraId);
        boolean result = strategyService.unbindCameraStrategy(cameraId);
        return ResponseResult.success(result);
    }

    @PostMapping("/set-global-default/{strategyId}")
    @Operation(summary = "设置全局默认策略", description = "设置指定策略为全局默认策略")
    public ResponseResult<Boolean> setGlobalDefaultStrategy(
            @Parameter(description = "策略ID") @PathVariable Long strategyId) {
        
        log.info("设置策略 {} 为全局默认策略", strategyId);
        boolean result = strategyService.setAsGlobalDefault(strategyId);
        return ResponseResult.success(result);
    }

    @GetMapping("/global-default")
    @Operation(summary = "获取全局默认策略", description = "获取当前的全局默认存储策略")
    public ResponseResult<HwCameraStorageStrategy> getGlobalDefaultStrategy() {
        
        log.info("获取全局默认策略");
        HwCameraStorageStrategy strategy = strategyService.getGlobalDefaultStrategy();
        return ResponseResult.success(strategy);
    }
}